import {
  cssVar_dom_posi_left,
  cssVar_dom_posi_top,
} from '../Css/DataDefine/sourceData';
import { set_dragEl_top_left } from '../Func/drag_dom/Classes/cla_drag_dom_operate';
import { normalizeContainer } from '../Func/normalizeContainer';

const win_session_dom_posi_tag = 'win_session_dom_posi';

/**
 * 实时 top 数据
 */
let top: string;

/**
 * 实时 left 数据
 */
let left: string;

/**
 * 由于 quick_func_float 是 全局 性的 因此 采用 sessionStorage 记录 相关数据
 */
export class win_session_dom_posi {
  get [Symbol.toStringTag]() {
    return win_session_dom_posi_tag;
  }

  constructor(dom: HTMLElement, spacename: string) {
    this.element = normalizeContainer(dom)!;

    this.spacename_top = `${spacename}_top`;

    this.spacename_left = `${spacename}_left`;
  }

  element;

  spacename_top;

  spacename_left;

  /**
   * 设定 win_load top left 信息
   */
  set_cur_posi() {
    top = this.element.style.getPropertyValue(cssVar_dom_posi_top);
    left = this.element.style.getPropertyValue(cssVar_dom_posi_left);

    sessionStorage.setItem(this.spacename_top, top);
    sessionStorage.setItem(this.spacename_left, left);
  }

  /**
   * 获取 win_load 上的 top left 信息
   * @returns
   */
  get_win_load_posi() {
    top = sessionStorage.getItem(this.spacename_top)!;
    left = sessionStorage.getItem(this.spacename_left)!;

    return { top, left };
  }

  /**
   * 装载 当前 win_load 位置信息
   * @param dom 指定 元素 设置 位置值
   */
  init_win_load_posi(dom: HTMLElement) {
    ({ top, left } = this.get_win_load_posi());

    if (top != null || left != null) {
      top = top || '0';
      left = left || '0';
      set_dragEl_top_left(dom, Number(top), Number(left));
    }
  }
}
